package org.infosec.lc.exercises.esper;

import org.infosec.lc.exercises.esper.modle.AEvent;
import org.infosec.lc.exercises.esper.modle.BEvent;
import org.infosec.lc.exercises.esper.modle.NormalizedEvent;
import org.infosec.lc.exercises.esper.process.EsperProcessESP;

/**
	文件名：EsperMian.java
	作  者：林超
	时  间：2011-6-22下午02:29:48

	功能描述：Esper 主启动类
	
	pom.xml	:
			<dependency>
	            <groupId>esper</groupId>
	            <artifactId>esper</artifactId>
	            <version>3.0</version>
        	</dependency>
        	
        	如无此包：Caused by: java.lang.ClassNotFoundException: org.antlr.runtime.tree.TreeNodeStream
        	<dependency>
	            <groupId>org.antlr</groupId>
	            <artifactId>antlr-runtime</artifactId>
	            <version>3.1.1</version>
        	</dependency>
			
 */
public class EsperMian {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		EsperProcessESP esper = new EsperProcessESP();
		TestEsp(esper);
//		TestCep(esper);
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private static void TestCep(EsperProcessESP esper) {
//		String pattern="every a=AEvent->b=BEvent(underlying!=a.underlying)";
		/**
		 * underlying in ['AAAAAAAAAAAAAAA','BBBBBBBBBBBBBBB']
		 * 		String 之间用","
		 * 		Integer 之间用":"
		 * 
		 * 		every ：如果没有every时整个操作模式表达式匹配模式匹配成功一次时停止。
		 * 				如果 有 every时一个新的模式匹配的开始侦听同一事件或事件集更多出现。 
		 */
		String pattern="every a=AEvent->b=BEvent(underlying in ('AAAAAAAAAAAAAAA','CCCCCCCCCCCCCCC','BBBBBBBBBBBBBBB'))";
//		String pattern="every (a=AEvent->b=BEvent(grade in [1:3)))where timer:within(1)";
//		String pattern="every (a=AEvent->b=BEvent) and not NormalizedEvent";
//		String pattern="timer:at(1,*,*,*,*)";
		esper.ProcessCep(pattern);
		
		AEvent a = new AEvent();
		a.setUnderlying("AAAAAAAAAAAAAAA");
		BEvent b = new BEvent();
		b.setUnderlying("BBBBBBBBBBBBBBB");
		b.setGrade(2);
		esper.sendEvent(b);
		esper.sendEvent(a);NormalizedEvent event = new NormalizedEvent();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		esper.sendEvent(b);
//		esper.sendEvent(event);
		esper.sendEvent(a);
		esper.sendEvent(b);
		esper.sendEvent(a);
		esper.sendEvent(b);
	}

	private static void TestEsp(EsperProcessESP esper) {
		esper.ProcessEsp();
//		esper.reset();	//这将停止并删除在esperEngine的所有语句{ engine.initialize(); }

		NormalizedEvent event = new NormalizedEvent();
		event.setThreatrank(6);
		NormalizedEvent event1 = new NormalizedEvent();
		event1.setThreatrank(8);
		int j = 0;
		outer:while(true){
			j++;
			if(j>100){
				break outer;
			}
			esper.sendEvent(event);esper.sendEvent(event1);
			
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}

